/**
 * Created by Bowa on 2014/8/28.
 */
var shoplist = [];
var goodlist = [];
var focusobj = null;
var focushop = null;
var focuspost = null;
var gouwuche = "gouwuche";
var postslist = [];
var _yanzhengma = "";
var focuslist = null;
var chattimmer = null;
var iftobutton = true;
var path = "";
var isRecordStart = false;
var counttimmer = null;
var count = 0;

var _chattype = 1;//1朋友聊天,2群聊天
var _mingancis = null;

var focusshetuan = null;
var shetuanlist = null;
var _replay3tpl = {};
_replay3tpl.tpl = '<li><a>'+
    '<img src="'+fileurl+'%s">'+
    '<h2>%s</h2>'+
    '<p>%s</p>'+
    '</a><a href="#" onclick="deleteReplay(%s)"></a></li>';
_replay3tpl.colums = ["img","username","ndate","id"];


var _replay3tpl2 = {};
_replay3tpl2.tpl = '<li>'+
    '<img src="'+fileurl+'%s">'+
    '<h2>%s</h2>'+
    '<p>%s</p>'+
    '</li>';
_replay3tpl2.colums = ["img","username","ndate"];




$(function(){
//设置类别列表
    var p666 = {};
    p666.tpl = '<li onclick="noticeDetail(%s)">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</li>';
    p666.colums = ["id","title","note"];
    $("#noticelist").data("property",JSON.stringify(p666));

    var p5 = {};
    p5.tpl = '<li><a href="#" onclick="postDetail(%s);">'+
        //'<img src="'+fileurl+'%s">'+
    '<h2>%s</h2>'+
    '<p>%s</p>'+
        '<p>%s   %s</p>'+
    '</a></li>';
    p5.colums = ["id","title","note","username","ndate"];
    $("#posts").data("property",JSON.stringify(p5));


    var p52 = {};
    p52.tpl = '<li onclick="postDetail2(%s);">'+
        //'<img style="height: 80px;" src="'+fileurl+'%s">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</li>';
    p52.colums = ["id","title","ndate"];
    $("#posts2").data("property",JSON.stringify(p52));


    var p523 = {};
    p523.tpl = '<li><a href="#" onclick="postDetail3(%s);">'+
        //'<img style="height: 80px;" src="'+fileurl+'%s">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '<p>时间:%s</p>'+
        '</a></li>';
    p523.colums = ["id","title","note","ptitle"];
    $("#posts3").data("property",JSON.stringify(p523));

    var p6 = {};
    p6.tpl = '<li><a href="#" onclick="">'+
    '<h2>%s</h2>'+
    '<p>%s</p>'+
    '<p>%s</p>'+
    '</a></li>';
    p6.colums = ["ndate","note","username"];
    $("#replays").data("property",JSON.stringify(p6));
    $("#replays2").data("property",JSON.stringify(p6));


    //clickYanzhengma();


    var p4 = {};
    p4.tpl = '<li><a onclick="toQunInfo(%s);">'+
        //'<img src="'+fileurl+'%s" style="height: 80px;">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</a></li>';
    p4.colums = ["id","title","note"];
    $("#myqunzulist").data("property",JSON.stringify(p4));



    var pshetuan = {};
    pshetuan.tpl = '<li onclick="toQunInfo(%s);">'+
        //'<img src="'+fileurl+'%s" style="height: 80px;">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</li>';
    pshetuan.colums = ["id","title","note"];

    $("#myqunzuadminlist").data("property",JSON.stringify(pshetuan));


    var huodongtpl = {};
    huodongtpl.tpl = '<li onclick="huodongdetail(%s);">'+
        //'<img src="'+fileurl+'%s" style="height: 80px;">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</li>';
    huodongtpl.colums = ["id","title","note"];
    $("#huodonglist").data("property",JSON.stringify(huodongtpl));




    var pp = {};
    pp.tpl = '<li onclick="toQunInfo(%s);">'+
        '<h2>%s %s</h2>'+
        '<p>部门领导:%s</p>'+
        '<p>%s</p>'+
        '</li>';
    pp.colums = ["id","title","daima","username","note"];
    $("#qunlist").data("property",JSON.stringify(pp));

    var pp2 = {};
    pp2.tpl = '<li onclick="toQunInfo(%s);">'+
        //'<img src="'+fileurl+'%s" style="height: 80px;">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</li>';
    pp2.colums = ["id","title","note"];
    $("#qunlist2").data("property",JSON.stringify(pp2));

    var ppp = {};
    ppp.tpl = '<li>'+
        '<p>%s</p>'+
        '<p onclick="toUserInfo(%s)" style="color: blue;">%s</p>'+
        '<p>%s</p>'+
        //'<p onclick="zan(this,%s);" style="color: red;"><span>%s</span>赞</p>'+
        '</li>';
    ppp.colums = ["note","uid","username","ndate"];
    $("#qunmsglist").data("property",JSON.stringify(ppp));


    var pppp = {};
    pppp.tpl = '<li>'+
        '<p>%s</p>'+
        '<p>%s</p>'+
        '<p>%s</p>'+
        '</li>';
    pppp.colums = ["note","username","ndate"];
    $("#msglist").data("property",JSON.stringify(pppp));






    var fp = {};
    fp.tpl = '<li><a  onclick="toUserInfo(%s,1);">'+
        '<img src="'+fileurl+'%s">'+
        '<h2>%s %s</h2>'+
        '<p>%s</p>'+
        '</a></li>';
    fp.colums = ["id","img","username","fname","sex"];
    $("#myfriendlist").data("property",JSON.stringify(fp));



    var fpt = {};
    fpt.tpl = '<li><a  onclick="toUserInfo(%s,1);">'+
        '<img src="'+fileurl+'%s">'+
        '<h2>%s %s</h2>'+
        '<p>迟到:%s</p>'+
        '<p>缺勤:%s</p>'+
        '</a></li>';
    fpt.colums = ["id","img","username","fname","chidao","queqin"];
    $("#usertongji").data("property",JSON.stringify(fpt));





    var fpp = {};
    fpp.tpl = '<li onclick="toUserInfo(%s);">'+
        '<img src="'+fileurl+'%s">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '<p>%s</p>'+
        '</li>';
    fpp.colums = ["id","img","username","address","sex"];
    $("#userlist").data("property",JSON.stringify(fpp));

    var fppp = {};
    fppp.tpl = '<li onclick="toYzMessage(%s);">'+
        '<img src="'+fileurl+'%s">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</li>';
    fppp.colums = ["id","img","fuser","ndate"];
    $("#messagelist").data("property",JSON.stringify(fppp));


    var atpl = {};
    atpl.tpl = '<li><a href="#" onclick="toAddressMg(%s);">'+
        '<h2>%s</h2>'+
        '</a><a onclick="delAddress(%s);"></a></li>';
    atpl.colums = ["id","title","id"];
    $("#addresss").data("property",JSON.stringify(atpl));


    $("#filebutton").unbind("click").bind("click",function(){
        sendFile();
    });


    $("#recordbtn").unbind("click").bind("click",function(){
        if(isRecordStart){
            isRecordStart = false;
            hideLoader();
            $("#recordbtn").text("点击录音");
            path = myObj.getRecordFile();
            setTimeout(function(){
                sendMsg(path,2);
            },2000);
            clearInterval(counttimmer);
            count = 0;
            myObj.stopSound();
        }else{
            isRecordStart = true;
            $("#recordbtn").text("录音中");
            //showLoader("录音中 "+count+"s",true);
            counttimmer = setInterval(function(){
                //showLoader("录音中 "+count+"s",true);
                $("#recordbtn").text("录音中"+count+"s");
                count++;
            },1000);
            myObj.recordSound();
        }

    });

    ajaxCallback("listJ",{table:"type"},function(data){
        _mingancis = data;
    });

});

function toMain(){
    toNotice();
    if(userinfo.roletype=="3"){
        $(".teacher").show();
    }else{
        $(".teacher").hide();
    }
}


function listFav(keystr){
    var pp = {};
    pp.tpl = '<li><a onclick="toQunInfo(%s);">'+
        '<h2>%s</h2>'+
        '<p>%s</p>'+
        '</a><a onclick="removeFav(%s,\'_shetuan\');"></a></li>';
    pp.colums = ["id","title","note","id"];
    $("#qunlist").data("property",JSON.stringify(pp));

    var huodongtpl = {};
    huodongtpl.tpl = '<li><a  onclick="huodongdetail(%s);">'+
        '<img src="'+fileurl+'%s" style="height: 80px;">'+
        '<h2>%s</h2>'+
        '<p>%s</p></a><a onclick="removeFav(%s,\'_huodong\');"></a>'+
        '</li>';
    huodongtpl.colums = ["id","img","title","note","id"];

    if(keystr=="_shetuan"){
        $("#favlist").data("property",JSON.stringify(pp));

    }else{
        $("#favlist").data("property",JSON.stringify(huodongtpl));
    }
    var list = getFavList(keystr);
    focuslist = list;
    if(keystr=="_shetuan"){
        shetuanlist = list;
    }
    $("#favlist").refreshShowListView(list);
}

function toMyFav(){
    changePage('favpage');
    listFav('_shetuan');
}

function toShetuan(){
    if(userinfo.roletype=="1"){
        toShetuanmg();
    }else{
        toQunzu();
    }
}


function listJiaoshi() {
    ajaxCallback("listJ",{table:"jiaoshi"},function (data) {

        focuslist = data;
        var p666 = {};
        p666.tpl = '<li onclick="jiaoshiDetail(%s)">'+
            '<h2>%s</h2>'+
            '<p>%s</p>'+
            '</li>';
        p666.colums = ["id","title","note"];
        $("#jiaoshilist").data("property",JSON.stringify(p666));
        $("#jiaoshilist").refreshShowListView(data);
    })
}

function jiaoshiDetail(id){
    var obj = getObjectById(id,focuslist);
    focusobj = obj;
    changePage('jiaoshidetailpage');
    $("#vtitle2").text(obj.title);
    $("#vnote2").text(obj.note);
}

function toJiaoshi(){
    changePage('jiaoshipage');
    listJiaoshi();
}


function tijiao(){
    changePage('infopage2');
}

function tijiaoYuyue(){
    var fdata = serializeObject($("#yuyueform"));
    fdata.uid = userinfo.id;
    fdata.username = userinfo.username;
    fdata.table = "yuyue";
    fdata.statecn="待审核";
    fdata.ndate = getNowTime();
    ajaxCallback("saveJ",fdata,function (data) {
        toYuyue();
    })
}

function toYuyue(){
    changePage('yuyuepage');
    var uid = null;
    if(userinfo.roletype=="2"){
        uid = userinfo.id;
    }
    ajaxCallback("listJ",{table:"yuyue",uid:uid},function (data) {
        var p666 = {};
        p666.tpl = '<li onclick="yuyueDetail(%s);">'+
            '<h2>会议室:%s</h2>'+
            '<p>预约说明:%s</p>'+
            '<p>申请人:%s ,预约时间:%s</p>'+
            '<p>状态;%s</p>'+
            '</li>';
        p666.colums = ["id","title","note","username","tdate","statecn"];
        $("#yuyuelist").data("property",JSON.stringify(p666));
        $("#yuyuelist").refreshShowListView(data);
    })
}

function yuyueDetail(id){
    ajaxCallback("findJ",{id:id,table:"yuyue"},function (data) {
        focusobj = data;
        changePage('yuyuedetailpage');
        $("#ytitle").text("预约会议室:"+focusobj.title);
        $("#ynote").text("预约理由:"+focusobj.note);
        $("#yusername").text("申请人:"+focusobj.username);
        $("#ytdate").text("预约时间:"+focusobj.tdate);
        $("#yndate").text("提交时间:"+focusobj.ndate);
        $("#ystatecn").text("状态:"+focusobj.statecn);
        $("#approvectn").hide();
        if(userinfo.roletype=="3"){
            $("#approvectn").show();
        }
    });
}


function approveYuyue(){
    var statecn = $("#yystatecn").val();
    ajaxCallback("saveJ",{table:"yuyue",id:focusobj.id,statecn:statecn},function (data) {
        focusobj.statecn = statecn;
        $("#ystatecn").text("状态:"+focusobj.statecn);
    })
}

var focuslist = null;
function toNotice(){
    changePage('noticepage');
    listNotice();
}
function listNotice(){
    ajaxCallback("listJ",{table:"notice"},function(data){
        focuslist = data;
        $("#noticelist").refreshShowListView(data);
    });
}

function noticeDetail(id){
    var obj = getObjectById(id,focuslist);
    changePage('noticedetailpage');
    $("#vtitle").text(obj.title);
    $("#vnote").text(obj.note);
    $("#vndate").text("时间:"+obj.ndate);
    if(obj.img){
        $("#dimg").attr("src",fileurl+obj.img);
    }else{
        $("#dimg").hide();
    }
}

function toShetuanmg(){
    if(userinfo.roletype=="1"){
        changePage('qunmgpage');
        listQunzu();
    }else{
        toQunzu();
    }

}


function toAddQunzu(){
    changePage('qunzumgpage');
    ajaxCallback("listJ",{table:"user"},function(data){
        $("#quid").refreshShowSelectMenu(data,"选择负责人","id","username");
    });
}





function saveQunzu(){
    var fdata = serializeObject($("#qunzuform"));
    fdata.uid = userinfo.id;
    fdata.username = userinfo.username;//$("#quid").find("option:selected").text();
    fdata.table="qunzu";
    uplaodImg(function (r){
        fdata.img = r;
        ajaxCallback("saveJ",fdata,function(data){
            ajaxCallback("findJ",{table:"user",id:fdata.uid},function(data2){
                data2.table="user";
                if(data2){
                    if(data2.qids){
                        data2.qids = data2.qids+","+data.info;
                    }else{
                        data2.qids = data.info;
                    }

                    ajaxCallback("saveJ",data2,function(r){//自动把负责人计入部门
                        ajaxCallback("findJ",{table:"user",id:userinfo.id},function(ruserinfo) {
                            userinfo = ruserinfo;
                            toShetuanmg();
                        })

                    });
                }
            });
        });
    });

}
function toQunInfo(id){
    var obj = getObjectById(id,shetuanlist);
    if(obj){
        focusshetuan = obj;
        focusobj = obj;
        changePage('qundetailpage');

        $("#candelshetuan").hide();
        if(userinfo.roletype=="1"){
            $("#canbuy").hide();
            $("#candel").hide();
            $("#candelshetuan").show();
            $("#candaohang").hide();
            $("#stdt").show();

        }else{
            checkIsMyQunzu();
        }
        $("#quntitle").text("名称:"+focusshetuan.title);
        $("#qundesc").text("介绍:"+focusshetuan.note);
        $("#qimg").attr("src",fileurl+focusshetuan.img);
        $("#qusername").text("部门领导:"+focusshetuan.username).attr("onclick","toUserInfo("+focusshetuan.uid+")");
    }

}

function delQun(){
    ajaxCallback("deleteJ",{id:focusshetuan.id,table:"qunzu"},function(data){
        toShetuanmg();
    });
}

function checkIsMyQunzu(cb){
    var qids = userinfo.qids || "";
    var flag = false;
    if(qids){
        var list = qids.split(",");
        for(var i=0;i<list.length;i++){
            if(list[i]==focusshetuan.id){
                flag = true;
                break;
            }
        }
    }
    if(flag){
        $("#canbuy").hide();
        $("#candel").show();
        $("#stdt").show();
    }else{
        if(focusshetuan.uid!=userinfo.id){
            $("#canbuy").show();
            $("#candel").hide();
            $("#stdt").hide();
        }else{
            $("#canbuy").hide();
            $("#candel").hide();
            $("#candaohang").hide();
            $("#stdt").show();
        }

    }
}


function sendAddQunMsg(){
    ajaxCallback("saveJ",{pid:focusshetuan.id,ptitle:focusshetuan.title,fid:userinfo.id,tid:focusshetuan.uid,status:"待同意",fuser:userinfo.username,img:userinfo.img,type:1,table:"yzmessage"},function(data){
        showLoader("请求已发送,等待负责人验证!",true);
    });
}

function addQunzu(){

    ajaxCallback("addQunzu",{uid:userinfo.id,qid:focusshetuan.id},function(data){
        userinfo = data;
        showTipTimer("操作成功!",function (){
            toQunzu();
        })
    });

}

function delQunzus(){
    ajaxCallback("delQunzus",{uid:userinfo.id,qid:focusshetuan.id},function(data){
        userinfo = data;
        toQunzu();
    });
}

function agreeQun(){
   /* ajaxCallback("addQunzu",{uid:focusobj.fid,qid:focusobj.pid},function(r){
        ajaxCallback("updateYzMsgStatus",{id:focusobj.id,status:"已同意"},function(data){
            toQunzu();
        });
    });*/

    ajaxCallback("findJ",{table:"user",id:focusobj.fid},function (data) {
        if(data){
            var qieds = data.qids;
            if(qieds){
                data.qids+=","+focusobj.pid;
            }else{
                data.qids = focusobj.pid;
            }
            data.table="user";
            ajaxCallback("saveJ",data,function(data2){
                userinfo = data2;
                focusobj.table="yzmessage";
                focusobj.status="已同意";
                ajaxCallback("saveJ",focusobj,function(data3){
                    showTipTimer("操作成功!",function (){
                        //toQunzu();
                    })
                });
            });
        }
    });
}

function refuseQun(){
    ajaxCallback("updateYzMsgStatus",{id:focusobj.id,status:"已拒绝"},function(data){
        goback();
    });

}
/**
 *
 */
function toHuodong(){
    changePage('huodongpage');
    if(userinfo.id==focusshetuan.uid){
        $("#hdaddbtn").show();
        $(".admin").show();
    }else{
        $("#hdaddbtn").hide();
        $(".admin").hide();
    }
    ajaxCallback("listJ",{pid:focusshetuan.id,table:"huodong"},function(data){
        focuslist = data;
        $("#huodonglist").refreshShowListView(data);
    });
}

function toAddHuodong(){
    changePage('huodongmgpage');
}

function saveHuodong(){
    var fdata = serializeObject($("#huotongform"));
    fdata.username = userinfo.username;
    fdata.uid = userinfo.id;
    fdata.pid = focusshetuan.id;
    fdata.ptitle = focusshetuan.title;
    fdata.table="huodong";
    ajaxFormUploadFile(function (r){
        fdata.img = r;
        ajaxCallback("saveJ",fdata,function(data){
            toHuodong();
        });
    });
}
function huodongdetail(id){
    var obj = getObjectById(id,focuslist);
    if(obj){
        focusobj = obj;
        changePage('huodongdetailpage');
        $("#htitle").text("名称:"+focusobj.title);
        $("#hdesc").text("说明:"+focusobj.note);
        $("#himg").hide();
        if(focusobj.img){
            $("#himg").attr("src",fileurl+focusobj.img);
        }

        if(focusobj.uid = userinfo.id){
            $("#replays3").data("property",JSON.stringify(_replay3tpl));
        }else{
            $("#replays3").data("property",JSON.stringify(_replay3tpl2));
        }
        listReplay3();
        //listHuodongUser();
    }

}


var beiwanglist = [];

function toAssist(){
    changePage("beiwangpage");
    listBeiwang();
}

function listBeiwang(){
    ajaxCallback("listJ",{uid:userinfo.id,table:"beiwang"},function(data){
        beiwanglist = data;
        var p7beiwang = {};
        p7beiwang.tpl = '<li><a href="#" onclick="toBeiwang(%s);">'+
            '<h2>%s</h2>'+
            '<p>%s</p>'+
            '</a></li>';
        p7beiwang.colums = ["id","title","ndate"];
        $("#beiwanglist").data("property",JSON.stringify(p7beiwang));
        $("#beiwanglist").refreshShowListView(data);
    });
}

function toAddBeiwang(){
    changePage("addBeiwangpage");
    $("#jishiaction").val("add");
}

function addBeiwang(){
    var fdata = serializeObject($("#beiwangform"));
    fdata.uid = userinfo.id;
    if(fdata.action=="edit"){
        fdata.id = focusobj.id;
    }

    fdata.table="beiwang";
    ajaxCallback("saveJ",fdata,function(){
        showTipTimer("操作成功!",function(){
            toAssist();
            var datestr = fdata.txdate+" "+fdata.txtime;
            var ddd = new Date(Date.parse(datestr.replace(/-/g,"/")));
            var times = ddd.getTime();
        });
    });
}
function toBeiwang(id){
    var o = getObjectById(id,beiwanglist);
    focusobj = o;
    changePage("beiwangdetail");
    $("#bwtitle").text("标题:"+ o.title);
    $("#bwnote").text("内容:"+ o.note);
    $("#bwndate").text("时间:"+ o.ndate);
    $("#txdatetime").text("提醒时间:"+o.txdate+" "+o.txtime);
}

function delBeiwang(){
    ajaxCallback("deleteJ",{id:focusobj.id,table:"beiwang"},function(){
        showTipTimer("操作成功!",function(){
            toAssist();
        });
    });
}

function toEditBeiwang(){
    changePage('addBeiwangpage');
    $("#jishiaction").val("edit");
    $("#txdate").val(focusobj.txdate);
    $("#txtime").val(focusobj.txtime);
    $("#title").val(focusobj.title);
    $("#note").val(focusobj.note);
}

function jiaruhuodong(){
    var fdata = {};
    fdata.pid = focusobj.id;
    fdata.ptitle = focusobj.title;
    fdata.uid = userinfo.id;
    fdata.username = userinfo.username;
    fdata.img = userinfo.img;
    fdata.type = 100;
    ajaxCallback("addReplay",fdata,function(data){
        listHuodongUser();
    });
}


function listHuodongUser(){
    ajaxCallback("listJ",{pid:focusobj.id,type:100,table:"replay"},function(data){
        $("#replays3").refreshShowListView(data);
    });
}

function deleteReplay(id){
    ajaxCallback("deleteJ",{id:id,table:"replay"},function(data){
        //listHuodongUser();
        listReplay3();
    });
}

/*****************************************************************好友管理********************************************/
function toMyFriend(){
    changePage('friendpage');
    $(".teacher").hide();
    if(focusshetuan.uid==userinfo.id){
        $(".teacher").show();
    }
    listMyFriend();
}

function dianming(){
    showLoader(focuslist[getRandom1(0,focuslist.length-1)]['fname'],true);
}
function listMyFriend(){
    ajaxCallback("listSqlJ",{sql:"select * from qq_user u where find_in_set('"+focusshetuan.id+"',u.qids) or u.id="+focusshetuan.uid},function(data){
        if(!data){
            $("#myfriendlist").html("").listview("refresh");
            return;
        }
        focuslist = data;
        conventFriendList(data,function (rlist){
            focuslist = rlist;
            //$("#myfriendlist").refreshShowListView(rlist);
            var html="";
            for(var i=0;i<focuslist.length;i++){
                var obj = focuslist[i];
                var username = obj.username;
                if(obj.title){
                    username= username+" "+obj.title;
                }

                /*if(obj.count&&obj.count>10){
                    username+=" (好朋友)";
                }else{
                    username+=" (好友)";
                }*/

                html+='<li><a  onclick="toUrl('+obj.id+');">'+
                    '<img src="'+fileurl+obj.img+'">'+
                    '<h2>'+username+'</h2>'+
                    '<p>'+obj.sex+'</p>'+
                    '</a></li>';
            }
            $("#myfriendlist").html(html).listview("refresh");
        });

    });
}

function toUserInfo(id,type){
    ajaxCallback("findJ",{id:id,table:"user"},function(obj){

        if(obj){
            focusobj = obj;
            changePage('userdetailpage');
            //checkIsMyFriend();
            $("#vusername2").text("用户名:"+focusobj.username);
            $("#vuserimg2").attr("src",fileurl+focusobj.img);
            $("#vbirth2").text("生日:"+focusobj.birth);
            $("#vsex2").text("性别:"+focusobj.sex);
            $("#vqq2").text("QQ:"+focusobj.qq);
            ajaxCallback("findJ",{uid:userinfo.id,fid:focusobj.id,table:"beizhu"},function(data){
                $("#beizhu").val(data.info || "");
            });
            if(focusshetuan.uid == userinfo.id && focusobj.id!=userinfo.id){
                $("#candelf").show();
            }else{
                $("#candelf").hide();
            }

        }
    });


}

function toSearchFriend(){
    changePage("addfriendpage");
    listUser();
}

function listUser(){
    ajaxCallback("listUser",{roletype:2},function(data){
        focuslist = data;
        $("#userlist").refreshShowListView(data);
    });
}

function addCount(){
    ajaxCallback("addCount",{uid:userinfo.id,fid:focusobj.id},function(data){

    });
}

function beizhu(){
    var data = {};
    data.uid = userinfo.id;
    data.fid = focusobj.id;
    data.title = $("#beizhu").val();
    ajaxCallback("addBeizhu",data,function(data){
        showLoader("操作成功",true);
    });
}

function conventFriendList(list,cb){
    ajaxCallback("listJ",{uid:userinfo.id,table:"beizhu"},function(data){
        if(list && list.length){
            if(data && data.length){
                for(var i=0;i<list.length;i++){
                    var id = list[i].id;
                    for(var j=0;j<data.length;j++){
                        var fid = data[j].fid;
                        if(fid==id){
                            list[i].title = data[j].title;
                            list[i].count = data[j].count;
                            continue;
                        }
                    }
                }
                cb && cb(list);
            }else{
                cb && cb(list);
            }
        }else{
            cb && cb(list);
        }
    });
}

function checkIsMyFriend(cb){
    ajaxCallback("checkIsMyFriend",{uid:userinfo.id,fid:focusobj.id},function(data){
        //cb && cb(data.info);
        if(data.info=="1"){
            $("#canadd").hide();
            $("#candelf").show();
        }else{
            if(focusobj.id!=userinfo.id){
                $("#canadd").show();
                $("#candelf").hide();
            }else{
                $("#canadd").hide();
                $("#candelf").hide();
            }

        }
    });
}

function addFriend(){
    /*ajaxCallback("addFriend",{uid:userinfo.id,fid:focusobj.id},function(data){
     userinfo = data;
     toMyFriend();
     });*/
    //改为同意的时候后台自动加上
    ajaxCallback("sendAddMessage",{fid:userinfo.id,tid:focusobj.id,status:"待同意",fuser:userinfo.username,img:userinfo.img},function(data){
        showLoader("请求已发送,等待好友验证!",true);
    });
}

function delFriend(){
    ajaxCallback("delFriend",{uid:userinfo.id,fid:focusobj.id},function(data){
        userinfo = data;
        toMyFriend();
    });
}


function toMyYanzhengMessage(){
    changePage('yzmessagelistpage');
    ajaxCallback("listJ",{tid:userinfo.id,table:"yzmessage"},function(data){
        focuslist = data;
        $("#messagelist").refreshShowListView(data);
    });
}

function toYzMessage(id){
    var msg = getObjectById(id,focuslist);
    if(msg){
        focusobj = msg;
        changePage('yzmsgdetailpage');
        $("#vusername4").text(msg.fuser);
        $("#vstatus").text("状态:"+msg.status);
        $("#userimg").attr("src",fileurl+msg.img);
        if(msg.status="待同意"){
            $("#opctn").show();
        }else{
            $("#opctn").hide();
        }
    }
}

function agree(){
    ajaxCallback("addFriend",{uid:focusobj.fid,fid:focusobj.tid},function(r){
        ajaxCallback("addFriend",{uid:userinfo.id,fid:focusobj.fid},function(rr){
            ajaxCallback("updateYzMsgStatus",{id:focusobj.id,status:"已同意"},function(data){
                userinfo = rr;
                toMyFriend();
            });

        });
    });
}

function refuse(){
    ajaxCallback("updateYzMsgStatus",{id:focusobj.id,status:"已拒绝"},function(data){
        goback();
    });

}




var addresslist = [];

/********************************************************计划增删改******************************************/
function toAddressPage(){
    changePage("addresspage");
    listAddress();
}
function listAddress(){
    ajaxCallback("listAddress",{uid:userinfo.id},function(data){
        addresslist = data;
        $("#addresss").refreshShowListView(data);
    });
}
function toAddressMg(id){
    changePage("addressmgpage");
    if(id){
        $("#addressid").val(id);
        $("#addressaction").val("edit");
        var obj = getObjectById(id,addresslist);
        focusobj = obj;
        $("#title").val(obj.title);
        $("#note").val(obj.note);
    }else{
        $("#addressform")[0].reset();
        $("#addressaction").val("add");
    }

}





function saveAddress(){
    var fdata = serializeObject($("#addressform"));
    fdata.uid = userinfo.id;
    ajaxCallback("saveAddress",fdata,function(){
        showTipTimer("操作成功",function(){
            toAddressPage();
        });
    });
}

function addAddress(){
    var fdata = {};
    fdata.uid = userinfo.id;
    fdata.title = userinfo.address;
    fdata.action = "add";
    ajaxCallback("saveAddress",fdata,function(){

    });
}


function delAddress(id){
    ajaxCallback("delAddress",{id:id},function(){
        toAddressPage();
        showLoader("操作成功",true);
    });
}


/*****************************************************************好友管理结束********************************************/





function backclear(){
    clearInterval(chattimmer);
    goback();
}

function toLuntan(id){
    changePage("luntanpage");
    listPosts(id);
}
function listPosts(id){
    ajaxCallback("listJ",{uid:id,type:1,pid:focusshetuan.id,table:"posts"},function(data){
        focuslist = data;
        $("#posts").refreshShowListView(data);
    });
}
function toAddForm(){
    changePage("addformpage");
}
function addForm(){
    var note = $("#fnote").val();
    var title = $("#ftitle").val();

    uplaodImg(function(r){
        ajaxCallback("saveJ",{ndate:getNdate(),pid:focusshetuan.id,ptitle:focusshetuan.title,uid:userinfo.id,title:title,note:note,username:userinfo.username,img:r,type:1,address:userinfo.address,statecn:"待审核",table:"posts"},function(){
            toLuntan();
        });
    });

}
function postDetail(id){
    var obj = getObjectById(id,focuslist);
    focuspost = obj;
    changePage("postdetail");
    $("#vptitle").text("标题:"+obj.title);
    $("#vpnote").text("内容:"+obj.note);
    $("#vpusername").text("请假人:"+obj.username);
    $("#vpdate").text("提交时间:"+obj.ndate);
    $("#statecnv").text("审核状态:"+obj.statecn);
    if(userinfo.roletype=="1" || userinfo.id==focusshetuan.uid){
        $(".teacher").show();
    }else{
        $(".teacher").hide();
    }
    //$("#vpdate12").text("位置:"+obj.address);
    if(obj.img){
        $("#pimg").attr("src",fileurl+obj.img);
        $("#imgctn").show();
    }else{
        $("#imgctn").hide();
    }
    if(obj.uid == userinfo.id){
        $("#mypost").show();
    }else{
        $("#mypost").hide();
    }
    listReplay();
}


function updatePostsStatecn(){
    var statecn = $("#statecn").val();
    focuspost.statecn = statecn;
    focuspost.table="posts";
    ajaxCallback("saveJ",focuspost,function(data){
        $("#statecnv").text("审核状态:"+statecn);
    });
}
function listReplay(){
    ajaxCallback("listJ",{pid:focuspost.id,table:"replay"},function(data){
        $("#replays").refreshShowListView(data);
    });
}
function addReplay(){
    var note = $("#rnote").val();
    ajaxCallback("saveJ",{pid:focuspost.id,uid:userinfo.id,username:userinfo.username,note:note,type:1,ndate:getNdate(),table:"replay"},function(data){
        listReplay();
        $("#rnote").val("");
    });
}



function toLuntan2(id){
    changePage("luntanpage2");
    if(userinfo.id==focusshetuan.uid){
        $("#hdaddbtn").show();
        $(".admin").show();
    }else{
        $("#hdaddbtn").hide();
        $(".admin").hide();
    }
    listPosts2(id);
}
function listPosts2(id){
    ajaxCallback("listJ",{pid:focusshetuan.id,type:2,table:"posts"},function(data){
        focuslist = data;
        $("#posts2").refreshShowListView(data);
    });
}
function toAddForm2(){
    changePage("addformpage2");
    myLocation.getLocation();
}
function addForm2(){
    var note = $("#fnote2").val();
    var title = $("#ftitle2").val();
    var latitude = _currentPosition.latitude;
    var longitude = _currentPosition.longitude;
    var address = _currentPosition.addr;
    var typetitle = $("#typetitle").val();
    var ndate = getNdate();
    //uplaodImg(function(r){
        ajaxCallback("saveJ",{table:"posts",uid:userinfo.id,title:title,note:note,username:userinfo.username,img:'',type:2,pid:focusshetuan.id,address:address,latitude:latitude,longitude:longitude,typetitle:typetitle,ndate:ndate},function(){
            toLuntan2();
        });
    //});

}


function downloadFile(){
    localFile();
}
var timeouttimmer = null;
var duetime = 60*10;
function postDetail2(id){
    //var obj = getObjectById(id,focuslist);
    ajaxCallback("findJ",{id:id,table:"posts"},function(data){
        var obj = data;
        focuspost = obj;
        changePage("postdetail2");
        $("#vptitle2").text("标识:"+obj.title);

        $("#vpnote2").text("考勤码:"+obj.note).hide();
        $("#vpusername2").text("部门领导:"+obj.username);
        $("#vpdate2").text("发布时间:"+obj.ndate);
        $("#vpdate22").text("位置:"+obj.address);
        if(obj.img){
            $("#pimg2").attr("src",fileurl+obj.img);
            $("#imgctn2").show();
        }else{
            $("#imgctn2").hide();
        }
        if(obj.uid == userinfo.id){
            $("#mypost2").show();
        }else{
            $("#mypost2").hide();
        }

        ajaxCallback("findJ",{id:focuspost.pid,table:"qunzu"},function (zunzu) {
            $("#kecheng").text("部门:"+zunzu.title);
            $("#jiaoshi").text("会议室:"+zunzu.didian);
            if(userinfo.id==focuspost.uid){
                $("#vpnote2").show();
            }
        });
        listReplay2(1);

       /* ajaxCallback("listSqlJ",{sql:"select * from qq_user u where find_in_set('"+focusshetuan.id+"',u.qids) or u.id="+focusshetuan.uid},function(data){
            ajaxCallback("listJ",{pid:focuspost.id,type:2,table:"replay"},function(data2){
                $("#qddetail").text("总共"+data.length+"人 已考勤:"+data2.length+"人");
                ajaxCallback("createQrcodeJ",{code:focuspost.id},function (data3) {
                        var img = data3.info;
                        $("#pimg2").attr("src",fileurl+img).show();
                })
            });

        });*/


        var fdate = str2Date(obj.ndate);
        var ftime = fdate.getTime();




        clearInterval(timeouttimmer);



        timeouttimmer = setInterval(function (){
            if(focuspost.statecn=="停止考勤"){
                $("#timeuplabel").text("停止考勤");
                clearInterval(timeouttimmer);
                return;
            }
            var ntime = Date.now();
            var cha = ntime/1000-ftime/1000;
            if(cha>duetime){
                $("#timeuplabel").text("超过考勤时间");
            }else if(cha<duetime && cha>=0){
                var resttime = duetime-cha;
                var lasttime = getTimeFromSeconds(resttime);
                $("#timeuplabel").text("倒计时:"+(lasttime));
            }else{
                $("#timeuplabel").text("未开始考勤");
            }
        },1000)

    });

}

function refreshQdcount(type){
    ajaxCallback("listSqlJ",{sql:"select * from qq_user u where find_in_set('"+focusshetuan.id+"',u.qids) or u.id="+focusshetuan.uid},function(data){
        ajaxCallback("listJ",{pid:focuspost.id,type:2,table:"replay"},function(data2){
            $("#qddetail").text("总共"+data.length+"人 已考勤:"+data2.length+"人");
            var html="";
            if(type==1){
                _focusqdlist = data2;
                for(var i=0;i<_focusqdlist.length;i++){
                    var obj = _focusqdlist[i];
                    if(obj.img){
                        html+='<li>'+
                            '<img style="height: 80px;" src="'+fileurl+obj.img+'">'+
                            '<h2>'+obj.username+'</h2>'+
                            '<p>'+(obj.note||"")+'</p>'+
                            '<p>'+(obj.ndate||"")+'</p>'+
                            '</li>';
                    }else{
                        html+='<li>'+
                            '<h2>'+obj.username+'</h2>'+
                            '<p>'+(obj.note||"")+'</p>'+
                            '<p>'+(obj.ndate||"")+'</p>'+
                            '</li>';
                    }

                }
            }else{
                _focusqdlist = [];
                for(var i=0;i<data.length;i++){
                    var flag = true;
                    for(var j=0;j<data2.length;j++){
                        if(data[i].id==data2[j].uid){
                            flag = false;
                        }
                    }
                    if(flag){
                        _focusqdlist.push(data[i]);
                    }
                }
                for(var i=0;i<_focusqdlist.length;i++){
                    var obj = _focusqdlist[i];
                    if(obj.img){
                        html+='<li onclick="toSetStatus('+obj.id+');">'+
                            '<img style="height: 80px;" src="'+fileurl+obj.img+'">'+
                            '<h2>'+obj.username+'</h2>'+
                            '<p>'+(obj.note||"")+'</p>'+
                            '<p>'+(obj.ndate||"")+'</p>'+
                            '</li>';
                    }else{
                        html+='<li onclick="toSetStatus('+obj.id+');">'+
                            '<h2>'+obj.username+'</h2>'+
                            '<p>'+(obj.note||"")+'</p>'+
                            '<p>'+(obj.ndate||"")+'</p>'+
                            '</li>';
                    }

                }
            }



            $("#replays2").html(html).listview('refresh');
        });

    });
}

var _focusuid = null;
function toSetStatus(uid){
    if(focusshetuan.uid==userinfo.id){
        _focusuid = uid;
        changePage('setstatuspage');
    }


}

function saveReplayBeizhu(){
    var note = $("#rnote").val();
    ajaxCallback("findJ",{table:"replay",type:101,pid:focuspost.id,uid:_focusuid},function (data) {
        if(data && data.id){
            data.note = note;
            data.ndate = getNdate();
            data.table="replay";
            ajaxCallback("saveJ",data,function (data) {
                showTipTimer("操作成功",function (){
                    goback();
                    refreshQdcount(2);
                })
            })
        }else{
            ajaxCallback("saveJ",{note:note,uid:_focusuid,pid:focuspost.id,ndate:getNdate(),table:"replay",type:101,address:focusshetuan.id},function (data) {
                showTipTimer("操作成功",function (){
                    goback();
                    refreshQdcount(2);
                })
            })
        }
    })

}

function toTongji(){
    changePage('tongjipage');
    tongji();
}

function tongji(){
    ajaxCallback("listSqlJ",{sql:"select * from qq_user u where find_in_set('"+focusshetuan.id+"',u.qids) or u.id="+focusshetuan.uid},function(data){
        focuslist = data;
        ajaxCallback("listJ",{table:"replay",sid:focusshetuan.id,type:101,address:focusshetuan.id},function (data2) {
            for(var i=0;i<focuslist.length;i++){
                focuslist[i]['queqin']=0;
                focuslist[i]['chidao']=0;
            }

            for(var i=0;i<focuslist.length;i++){
                for(var j=0;j<data2.length;j++){
                    if(focuslist[i].id==data2[j].uid){
                        if(data2[j].note=="迟到"){
                            focuslist[i].chidao = focuslist[i].chidao+1;
                        }else if(data2[j].note=="缺勤"){
                            focuslist[i].queqin = focuslist[i].queqin+1;
                        }
                    }
                }
            }
            $("#usertongji").refreshShowListView(focuslist);
        });

    });
}

function toFankui(){
    changePage('fankuipage');
}
function saveFankui(){
    var fdata = {};
    fdata.note = $("#fnote4").val();
    fdata.username = userinfo.username;
    fdata.ndate = getNdate();
    fdata.table="fankui";
    ajaxCallback("saveJ",fdata,function (data) {
        showLoader("操作成功!",true);
    })
}

var _focusqdlist = [];

function listReplay2(type){
    refreshQdcount(type);
    /*ajaxCallback("listJ",{pid:focuspost.id,type:2,table:"replay"},function(data){
        _focusqdlist = data;
        var html="";
        for(var i=0;i<data.length;i++){
            var obj = data[i];
            if(obj.img){
                html+='<li>'+
                    '<img style="height: 80px;" src="'+fileurl+obj.img+'">'+
                    '<h2>'+obj.username+'</h2>'+
                    '<p>'+obj.note+'</p>'+
                    '<p>'+obj.ndate+'</p>'+
                    '</li>';
            }else{
                html+='<li>'+
                    '<h2>'+obj.username+'</h2>'+
                    '<p>'+obj.note+'</p>'+
                    '<p>'+obj.ndate+'</p>'+
                    '</li>';
            }

        }
        $("#replays2").html(html).listview('refresh');
    });*/
}
function addReplay2(){
    if(focuspost.statecn=="停止考勤"){
        showLoader("已停止考勤!",true);
        return;
    }
    var fdate = str2Date(focuspost.ndate);
    var ftime = fdate.getTime();
    var ntime = Date.now();
    var cha = ntime/1000-ftime/1000;
    if(cha>duetime){
        showLoader("超过考勤时间!",true);
        return;
    }


    var qdcode =  $("#fnote5").val();
    if(qdcode!=focuspost.note){
        showLoader("考勤码不对!",true);
        return;
    }
    if(focuspost.statecn=="停止考勤"){
        showLoader("已停止考勤!",true);
        return;
    }
   var fdata = {};
    fdata.pid = focuspost.id;
    fdata.uid = userinfo.id;
    fdata.username = userinfo.username;
    fdata.type = 2;
    fdata.table="replay";
    fdata.img = userinfo.img;
    fdata.ndate = getNdate();

    var flag = false;
    for(var i=0;i<_focusqdlist.length;i++){
        var rp = _focusqdlist[i];
        if(rp.uid==userinfo.id){
            flag = true;
            break;
        }
    }

    if(flag){
        showLoader("你已经考勤过了!",true);
        return;
    }

    /*ajaxFormUploadFile(function (r){
        fdata.img = r;*/
        ajaxCallback("saveJ",fdata,function(data){
            listReplay2();
            /*$("#rnote2").val("");
            $("#qdaddress").val("");*/
            refreshQdcount();
        });
    //})

}


function stopPost(){
    ajaxCallback("saveJ",{table:"posts",id:focuspost.id,statecn:"停止考勤"},function (data) {
        focuspost.statecn = "停止考勤";
    })

}

function qiandao(){
    var fdata = {};
    fdata.pid = _focuscourseqiandao.id;
    fdata.uid = userinfo.id;
    fdata.username = userinfo.username;
    fdata.type = 2;
    fdata.note="正常考勤";
    fdata.table = "replay";
    fdata.ndate = getNdate()

    ajaxFormUploadFile(function (r){
        fdata.img = r;
        ajaxCallback("saveJ",fdata,function(data){
            showTipTimer("考勤成功!",function (){
               //postDetail2(_focuscourseqiandao.id);
            });
            /*$("#rnote2").val("");
            $("#qdaddress").val("");*/
        });
    })

}


function addReplay3(){
    var note = $("#rnote3").val();
    ajaxCallback("saveJ",{ndate:getNdate(),pid:focusobj.id,uid:userinfo.id,username:userinfo.username,note:note,type:3,img:userinfo.img,table:"replay"},function(data){
        listReplay3();
        $("#rnote3").val("");
    });
}

function listReplay3(){
    ajaxCallback("listJ",{pid:focusobj.id,type:3,table:"replay"},function(data){
        $("#replays3").refreshShowListView(data);
    });
}



function toLuntan3(id){
    changePage("luntanpage3");
    listPosts3(id);
}
function listPosts3(id){
    ajaxCallback("listJ",{uid:id,type:3,table:"posts"},function(data){
        focuslist = data;
        $("#posts3").refreshShowListView(data);
    });
}
function toAddForm3(){
    changePage("addformpage3");
}
function addForm3(){
    var note = $("#fnote3").val();
    var title = $("#ftitle3").val();
    var latitude = _currentPosition.latitude;
    var longitude = _currentPosition.longitude;
    var address = _currentPosition.addr;
    var fdata = serializeObject($("#luntanform3"));
    fdata.uid = userinfo.id;
    fdata.username = userinfo.username;
    fdata.type = 3;
    fdata.table="posts";
    uplaodImg(function(r){
        ajaxCallback("saveJ",fdata,function(){
            toLuntan3();
        });
    });

}
function postDetail3(id){
    var obj = getObjectById(id,focuslist);
    focuspost = obj;
    changePage("postdetail3");
    $("#vptitle3").text("标题:"+obj.title);
    $("#vpnote3").text("内容:"+obj.note);
    $("#vpusername3").text("发布者:"+obj.username);
    $("#vpdate3").text("时间:"+obj.ptitle);
    //$("#vpdate33").text("位置:"+obj.address);

    if(obj.uid == userinfo.id){
        $("#mypost3").show();
    }else{
        $("#mypost3").hide();
    }
}


function mypost(){
    toLuntan(userinfo.id);
}
function delPosts2(){
    ajaxCallback("deleteJ",{id:focuspost.id,table:"posts"},function(data){
        toLuntan();
    });
}

function delPosts(){
    ajaxCallback("deleteJ",{id:focuspost.id,table:"posts"},function(data){
        toLuntan2();
    });
}

function toQunzu(){

        changePage("qunpage");
        listMyQunzu();


}



function listQunzu(){
    ajaxCallback("listJ",{table:"qunzu"},function(data){
        shetuanlist = data;
        focuslist = data;
        $("#myqunzuadminlist").refreshShowListView(data);
    });
}

function listMyQunzu(){
    var qids = userinfo.qids||"";
    var sql = "select * from qq_qunzu qun where qun.id in ("+qids+") or qun.uid="+userinfo.id;
    if(qids==""){
        sql = "select * from qq_qunzu qun where qun.uid="+userinfo.id;
    }
    ajaxCallback("listSqlJ",{sql:sql},function(data){
        shetuanlist = data;
        focuslist = data;
        $("#myqunzulist").refreshShowListView(data);
    });

}


function toSearchQunzu(){
    changePage("addQunzupage");
    listQun();
}

/*function toShetuan(){
    changePage("shetuanpage");
    listQun2();
}*/




function refreshQun(el){
    var type = $(el).val();
    listQun(type);
}

function listQun(type){
    type = type || "";
    ajaxCallback("listJ",{type:type,table:"qunzu"},function(data){
        shetuanlist = data;
        focuslist = data;
        $("#qunlist").refreshShowListView(data);
    });
}





function listQun2(type){
    type = type || "";
    ajaxCallback("listQunzu",{type:type},function(data){
        shetuanlist = data;
        focuslist = data;
        $("#qunlist2").refreshShowListView(data);
    });
}



function sendQunMsg(){
    var note = $("#chatnote").val();
    var msg = {};
    msg.note = note;
    msg.qid = focusobj.id;
    msg.uid = userinfo.id;
    msg.username = userinfo.username;
    ajaxCallback("addQunMessage",msg,function(data){
        ajaxCallback("listQunMessage",{uid:userinfo.id,qid:focusobj.id},function(data){
            $("#chatnote").val("");
            $("#qunmsglist").refreshShowListView(data);
        });
    });
}

/*function sendMsg(){
    var note = $("#chatcontent").val();
    var msg = {};
    msg.note = note;
    msg.fid = focusobj.id;
    msg.uid = userinfo.id;
    msg.username = userinfo.username;
    ajaxCallback("addMessage",msg,function(data){
        ajaxCallback("listMyMessage",{uid:userinfo.id,fid:focusobj.id},function(data){
            $("#chatcontent").val("");
            $("#msglist").refreshShowListView(data);
        });
    });
}*/

function toQunChat(id){
    var qz = getObjectById(id,focuslist);
    focusobj = qz;
    changePage("chatpage");
    chattimmer = setInterval(function (){
        refreshServerQunChatList();
    },1000);
}

function toChat(id,type){
    if(userinfo.status=="禁言"){
        showLoader("你已被禁言",true);
        return;
    }
    _chattype = type;
    var qz = getObjectById(id,focuslist);
    focusobj = qz;
    changePage("chatpage");
    chattimmer = setInterval(function (){
        refreshServerChatList();
    },1000);
}

function refreshChatMSg(){
    ajaxCallback("listMyMessage",{uid:userinfo.id,fid:focusobj.id},function(data){
        $("#msglist").refreshShowListView(data);
    },true);
}

function zan(el,id){
    ajaxCallback("zan",{id:id},function(data){
        var showel = $(el).find("span").eq(0);
        $(showel).text(data.info);
    });
}







function sendFile(){
    getFileAttach(function(path){
        sendMsg(path,3);
    });
}

function sendMsg(path,type){

    var ajaxurl = "addMessage";
    if(_chattype==2){
        ajaxurl="addQunMessage";
    }
    var obj = {};
    obj.uid = userinfo.id;
    obj.fid = focusobj.id;
    if(_chattype==2){
        obj.qid = focusobj.id;
    }
    obj.note = $("#msgaarea").val();
    obj.username = userinfo.username;
    obj.fusername = focusobj.username;
    obj.type = 1;
    obj.img = userinfo.img;
    if(path){
        if(type==2){
            obj.type = type;
            uploadFileUrl = path;
            uplaodImg(function(r){
                obj.attach = r;
                ajaxCallback(ajaxurl,obj,function(data){
                    iftobutton = true;
                    refreshServerChatList();
                });
            });
        }else if(type==3){
            obj.type = type;
            uploadFileUrl = path;
            uplaodImg(function(r){
                obj.attach = r;
                obj.attachname = uploadFileUrl.substr(uploadFileUrl.lastIndexOf("/")+1);
                ajaxCallback(ajaxurl,obj,function(data){
                    iftobutton = true;
                    refreshServerChatList();
                });
            });
        }

    }else{
        if(checkMgc(obj.note)){
            showLoader("消息不合法",true);
            return;
        }
        obj.attach = "";
        ajaxCallback(ajaxurl,obj,function(data){
            iftobutton = true;
            refreshServerChatList();
        });
    }
    $("#msgaarea").val("");


    /*var note = $("#chatnote").val();
    var msg = {};
    msg.note = note;
    msg.qid = focusobj.id;
    msg.uid = userinfo.id;
    msg.username = userinfo.username;
    ajaxCallback("addQunMessage",msg,function(data){
        ajaxCallback("listQunMessage",{uid:userinfo.id,qid:focusobj.id},function(data){
            $("#chatnote").val("");
            $("#qunmsglist").refreshShowListView(data);
        });
    });*/
    addCount();
}

function checkMgc(str){
    for(var i=0;i<_mingancis.length;i++){
        var mgc = _mingancis[i]['title'];
        if(str.indexOf(mgc)!=-1){
            return true;
        }
    }
    return false;
}

function refreshServerChatList(){
    var queryobj = {uid:userinfo.id,fid:focusobj.id};
    var ajaxurl = "listMyMessage";
    if(_chattype==2){
        queryobj = {uid:userinfo.id,qid:focusobj.id};
        ajaxurl = "listQunMessage";
    }
    ajaxCallback(ajaxurl,queryobj,function(data){
        refreshChatList(data);
    },true);
}


var _oldlength = null;
function refreshChatList(data){
    if(data.length!=_oldlength){
        iftobutton = true;
        _oldlength = data.length;
    }
    var html = "";
    if(data && data.length){
        for(var i=0;i<data.length;i++){
            var msg = data[i];
            var cn = "citemto";
            if(msg.uid != userinfo.id){
                var cn = "citemfrom";
            }
            var li = "";
            if(msg.type==1){
                li = '<li class="'+cn+'"><div><img src="'+fileurl+msg.img+'"><p>'+msg.note+'</p></div></li>';
            }else if(msg.type==2){
                li = '<li class="'+cn+'" onclick="myObj.playAudio(\''+msg.attach+'\');"><div><img src="'+fileurl+msg.img+'"><p>点击播放语音</p></div></li>';
            }else if(msg.type==3){
                if(getFileType(msg.attach)==".png" || getFileType(msg.attach)==".jpg"||getFileType(msg.attach)==".gif"||getFileType(msg.attach)==".jpeg"){
                    li = '<li class="'+cn+'" onclick="localFile(\''+msg.attach+'\');"><div><img src="'+fileurl+msg.img+'"><p class="imgctn" style="background-image: url('+fileurl+msg.attach+')"></p></div></li>';
                }else{
                    li = '<li class="'+cn+'" onclick="localFile(\''+msg.attach+'\');"><div><img src="'+fileurl+msg.img+'"><p>'+msg.attachname+'</p></div></li>';
                }

            }

            html+=li;
        }
        $("#chatlist").html(html);
        setScroll("chatscroll",document.getElementById("chatdiv"));
    }else{
        $("#chatlist").html(html);
    }
    var hei = $("#chatlist").height();
    if(iftobutton){
        if(hei>600){
            var scrollY = 0-(hei-600);
            scrolls["chatscroll"].scrollTo(0,scrollY,300);
        }
        iftobutton = false;
    }


}


function toDitu(){
    toMapPage();
    setTimeout(function (){
        drawUserPoint(focusobj);
    },300);
}

function searchLine(){
    var ndate = $("#ndate").val();
    if(ndate){
        ajaxCallback("listRecord",{uid:focusobj.id,ndate:ndate},function(data){
            drawLine(data);
        });
    }else{
        showLoader("请录入日期",true);
    }

}